package com.atguigu.study.base.pool;

import java.util.concurrent.*;

/**
 * @Author 快点干早点散
 * @Create 2025/4/25 21:49
 * Description:
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService threadPoolExecutor = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors(),//获取当前最大核数
                Runtime.getRuntime().availableProcessors() + 1,
                1L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
                );
        try {
            for (int i = 1; i <=10; i++) {
                int finalI=i;
               threadPoolExecutor.execute(()->{
                   System.out.println(Thread.currentThread().getName() + "号业务员" + "为" + finalI + "号客户办理业务");
               });
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            threadPoolExecutor.shutdown();
        }
    }

    public static void defaultPool() {
        //        ExecutorService threadPool = Executors.newFixedThreadPool(5);//固定线池
//            ExecutorService threadPool=Executors.newSingleThreadExecutor();//一池一线程
        ExecutorService threadPool = Executors.newCachedThreadPool();//自动扩容自动配置 1池N线程


        try {
            for (int i = 1; i <= 10; i++) {
                int finalI = i;
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "号业务员" + "为" + finalI + "号客户办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }

    }
}
